<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/10/8
 * Time: 9:50
 */

namespace app\manage\controller;


class WeixinMsgReplay extends Base
{
    private $mod;
    public function _initialize(){
        parent::_initialize();

        $this->mod = model('weixin_msg_replay');
        $this->list_field = 'id,keyword,type,create_time,update_time,status';
    }
    protected function _search(){
        $map['event_type']   = 1;//自定义回复
        ($type = input('param.type',1)) && $map['type'] = $type;
        ($keyword = input('get.keyword')) && $map['keyword'] = ['like', '%'.$keyword.'%'];
        $status = -1;
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $status = input('get.status/d');
        }
        $status>=0 && $map['status'] = ['eq',$status];
        $this->assign('search', array(
            'status'  => $status,
            'keyword' => $keyword,
            'type'    => $type
        ));
        return $map;
    }
    public function add(){
        $type = input('param.type/d',1);
        $tpl  = 'add_'.$type;
        $this->assign('type',$type);
        $this->assign('model',$this->mod->getModel());//模型列表
        return $this->fetch($tpl);
    }
    public function addDo(){

        //后置事件 添加标签
        \app\common\model\WeixinMsgReplay::event('after_insert',function($obj){
            if(isset($obj->keyword)){
                $tag = explode(',',trim($obj->keyword,','));
                if($tag){
                    $data = [];
                    foreach($tag as $v){
                        $data[] = [
                            'aid'  => $obj->id,
                            'name' => trim($v)
                        ];
                    }
                    if($data){
                        model('tags')->saveAll($data);
                    }
                }
            }
            return true;
        });
        parent::addDo();
    }
    public function edit(){
        $type = input('param.type/d',1);
        $id   = input('param.id/d');
        if($id){
            $info = $this->mod->where(['id'=>$id])->field('id,aid,model,keyword,data,type')->find();
            $tpl  = 'edit_'.$type;
            $this->assign('info',$info);
            $this->assign('type',$type);
            $this->assign('model',$this->mod->getModel());//模型列表
            return $this->fetch($tpl);
        }else{
            $this->error('参数错误');
        }

    }
    public function editDo(){
        \app\common\model\WeixinMsgReplay::event('after_update',function($obj){
            if(isset($obj->keyword)){
                $tag = explode(',',trim($obj->keyword,','));
                if($tag){
                    $data = [];
                    foreach($tag as $v){
                        $data[] = [
                            'aid'  => $obj->id,
                            'name' => trim($v)
                        ];
                    }
                    if($data){
                        model('tags')->where(['aid'=>$obj->id])->delete();
                        model('tags')->saveAll($data,$data);
                    }
                }
            }
            return true;
        });
        parent::editDo();
    }
    public function ajaxGetList(){
        $pagesize = 10;
        $page = input('param.page/d',1);
        $keyword = input('param.keyword');
        $model   = input('param.model','article');
        $mod     = model($model);
        $field   = 'id,img';
        $map['status'] = 1;
        if($model == 'house'){
            $field .= ',concat(title," ",average_price,"元/㎡") as title,special_youhui as description';
        }else{
            $field .= ',title,description';
        }
        if($keyword){
            $map['title'] = ['like','%'.$keyword.'%'];
        }
        $count = $mod->where($map)->count('id');
        $lists =  $mod->field($field)->where($map)->page($page,$pagesize)->select();
        $return['total'] = ceil($count/$pagesize);
        $return['lists'] = $lists;
        if($lists){
            $this->ajaxReturn(1,'',$return);
        }else{
            $this->ajaxReturn(0,'无相关数据');
        }
    }

}